package xml;

import java.io.File;
import java.util.*;

import dataStructures.*;

public class DirectoryReader {
	
	private List<QueryAndResults> queryAndResults;
	
	public List<QueryAndResults> getQueryAndResults() {
		return queryAndResults;
	}
	
	public DirectoryReader() {
		queryAndResults = new ArrayList<QueryAndResults>();
		File folder = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "data");
		File[] listOfFiles = folder.listFiles();
				
		QueryReader queryReader = new QueryReader();
		TweetReader tweetReader = new TweetReader();
		MyQuery query = null;
		List<MyTweet> tweets = null;
		String date = null;
		
		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				//System.out.println("File " + listOfFiles[i].getName());
				String[] dateType = listOfFiles[i].getName().split("_");
				date = dateType[0];
				
				if (dateType[1].startsWith("query")) {
					query = queryReader.readXML(listOfFiles[i].toString());
					//System.out.println(query);
				} else if (dateType[1].startsWith("results")) {
					tweets = tweetReader.readXML(listOfFiles[i].toString());
					//System.out.println(tweets);
				}
				
				//when we get all data we save it to the structure
				if (query != null && tweets != null && date != null) {
					
					QueryAndResults queryAndResult = new QueryAndResults(query, tweets, date);
					//XXX: printing content of data directory
					//System.out.println(queryAndResult);
					queryAndResults.add(queryAndResult);
					
					query=null;
					tweets=null;
					date=null;
				}
				
			} 
//			else if (listOfFiles[i].isDirectory()) {
//				System.out.println("Directory " + listOfFiles[i].getName());
//			}	
		}
	}

	public static void main(String[] args) {
		new DirectoryReader();
	}
}